home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / macintosh-c / macc-carbon-demos-nonbinhex.sit / macc-carbon-demos-nonbinhex / chap18-demo-classic events / NewOpenCloseSave.c < prev    next >
Text File  |  2001-08-07  |  37KB  |  1,235 lines

  1. // *******************************************************************************************
  2. // NewOpenCloseSave.c
  3. // *******************************************************************************************
  4.  
  5. // ………………………………………………………………………………………………………………………………………………………………………………………………………………………… includes
  6.  
  7. #include "Files.h"
  8.  
  9. // …………………………………………………………………………………………………………………………………………………………………………………………………… global variables
  10.  
  11. NavDialogRef    gModalToApplicationNavDialogRef;
  12. SInt16                gCurrentNumberOfWindows = 0;
  13. Rect                    gDestRect, gViewRect;
  14. Boolean                gCloseDocWindow = false;
  15.  
  16. extern NavEventUPP    gGetFilePutFileEventFunctionUPP;
  17. extern SInt16                gAppResFileRefNum;
  18. extern Boolean            gDone;
  19. extern Boolean            gQuittingApplication;
  20. extern Boolean            gRunningOnX;
  21.  
  22. // ****************************************************************************** doNewCommand
  23.  
  24. OSErr  doNewCommand(void)
  25. {
  26.     OSErr         osError = noErr;
  27.     WindowRef    windowRef;
  28.     OSType        documentType = kFileTypeTEXT;
  29.  
  30.     osError = doNewDocWindow(true,documentType,&windowRef);
  31.  
  32.     if(osError == noErr)
  33.         SetWindowProxyCreatorAndType(windowRef,kFileCreator,kFileTypeTEXT,kUserDomain);             /////
  34.  
  35.     return osError;
  36. }
  37.  
  38. // ***************************************************************************** doOpenCommand
  39.  
  40. OSErr  doOpenCommand(void)
  41. {
  42.     OSErr                                            osError = noErr;
  43.     NavDialogCreationOptions    dialogOptions;
  44.     Str255                                        applicationName;
  45.     NavTypeListHandle                    fileTypeListHdl = NULL;
  46.  
  47.     // ………………………………………………………………………………………………………………………………………… create application-modal Open dialog
  48.  
  49.     osError = NavGetDefaultDialogCreationOptions(&dialogOptions);
  50.     if(osError == noErr)
  51.     {
  52.         GetIndString(applicationName,rMiscStrings,sApplicationName);
  53.         dialogOptions.clientName = CFStringCreateWithPascalString(NULL,applicationName,
  54.                                                                                                                             CFStringGetSystemEncoding());
  55.         dialogOptions.modality = kWindowModalityAppModal;
  56.         fileTypeListHdl = (NavTypeListHandle) GetResource('open',rOpenResource);
  57.  
  58.         osError = NavCreateGetFileDialog(&dialogOptions,fileTypeListHdl,
  59.                                                                          gGetFilePutFileEventFunctionUPP,NULL,NULL,NULL,
  60.                                                                          &gModalToApplicationNavDialogRef);
  61.         if(osError == noErr && gModalToApplicationNavDialogRef != NULL)
  62.         {
  63.             osError = NavDialogRun(gModalToApplicationNavDialogRef);
  64.             if(osError != noErr)
  65.             {
  66.                 NavDialogDispose(gModalToApplicationNavDialogRef);
  67.                 gModalToApplicationNavDialogRef = NULL;
  68.             }
  69.         }
  70.         
  71.         if(dialogOptions.clientName != NULL)
  72.             CFRelease(dialogOptions.clientName);
  73.  
  74.         if(fileTypeListHdl != NULL)
  75.             ReleaseResource((Handle) fileTypeListHdl);
  76.     }
  77.  
  78.     return osError;
  79. }
  80.  
  81. // **************************************************************************** doCloseCommand
  82.  
  83. OSErr  doCloseCommand(NavAskSaveChangesAction action)
  84. {
  85.     WindowRef                        windowRef;
  86.     SInt16                            windowKind;
  87.     docStructureHandle    docStrucHdl;
  88.     OSErr                                osError = noErr;
  89.  
  90.     windowRef = FrontWindow();
  91.     windowKind = GetWindowKind(windowRef);
  92.  
  93.     switch(windowKind)
  94.     {
  95.         case kApplicationWindowKind:
  96.             docStrucHdl = (docStructureHandle) GetWRefCon(windowRef);
  97.  
  98.             // …………………………………………… if window has unsaved changes, create Save Changes alert and return
  99.  
  100.             if((*docStrucHdl)->windowTouched == true)
  101.             {
  102.                 if(IsWindowCollapsed(windowRef))
  103.                     CollapseWindow(windowRef,false);
  104.  
  105.                 osError = doCreateAskSaveChangesDialog(windowRef,docStrucHdl,action);
  106.             }
  107.  
  108.             // ……………………………………………………………………………………………………………………………………… otherwise close file and clean up
  109.  
  110.             else
  111.                 osError = doCloseDocWindow(windowRef);
  112.             break;
  113.  
  114.         case kDialogWindowKind:
  115.             // Hide or close modeless dialog, as required.
  116.             break;
  117.     }
  118.  
  119.     return osError;
  120. }
  121.  
  122. // ***************************************************************************** doSaveCommand
  123.  
  124. OSErr  doSaveCommand(void)
  125. {
  126.     WindowRef                        windowRef;
  127.     docStructureHandle    docStrucHdl;
  128.     OSErr                                osError = noErr;
  129.     Rect                                portRect;
  130.         
  131.     windowRef = FrontWindow();
  132.     docStrucHdl = (docStructureHandle) GetWRefCon(windowRef);
  133.  
  134.     // ……… if the document has a file ref number, write the file, otherwise call doSaveAsCommand
  135.  
  136.     if((*docStrucHdl)->fileRefNum)
  137.     {
  138.         osError = doWriteFile(windowRef);
  139.  
  140.         SetPortWindowPort(windowRef);
  141.         GetWindowPortBounds(windowRef,&portRect);
  142.         EraseRect(&portRect);
  143.         InvalWindowRect(windowRef,&portRect);
  144.     }        
  145.     else
  146.         osError = doSaveAsCommand();
  147.  
  148.     if(osError == noErr)                                                                                                                                     /////
  149.         SetWindowModified(windowRef,false);                                                                                                     /////
  150.  
  151.     return osError;
  152. }
  153.  
  154. // *************************************************************************** doSaveAsCommand
  155.  
  156. OSErr  doSaveAsCommand(void)
  157. {
  158.     OSErr                                            osError = noErr;
  159.     NavDialogCreationOptions    dialogOptions;
  160.     WindowRef                                    windowRef;
  161.     Str255                                        windowTitle, applicationName;
  162.     docStructureHandle                docStrucHdl;
  163.     OSType                                        fileType;
  164.  
  165.     // ……………………………………………………………………………………………………………………………… create window-modal Save Location dialog
  166.  
  167.     osError = NavGetDefaultDialogCreationOptions(&dialogOptions);
  168.     if(osError == noErr)
  169.     {
  170.         dialogOptions.optionFlags ^= kNavAllowStationery;
  171.  
  172.         windowRef = FrontWindow();
  173.  
  174.         GetWTitle(windowRef,windowTitle);
  175.         dialogOptions.saveFileName = CFStringCreateWithPascalString(NULL,windowTitle,
  176.                                                                                                                                 CFStringGetSystemEncoding());
  177.         GetIndString(applicationName,rMiscStrings,sApplicationName);
  178.         dialogOptions.clientName = CFStringCreateWithPascalString(NULL,applicationName,
  179.                                                                                                                             CFStringGetSystemEncoding());
  180.         dialogOptions.parentWindow = windowRef;
  181.         dialogOptions.modality = kWindowModalityWindowModal;
  182.         
  183.         docStrucHdl = (docStructureHandle) GetWRefCon(windowRef);
  184.         if((*docStrucHdl)->editStrucHdl != NULL)
  185.             fileType = kFileTypeTEXT;
  186.         else if((*docStrucHdl)->pictureHdl != NULL)
  187.             fileType = kFileTypePICT;
  188.  
  189.         osError = NavCreatePutFileDialog(&dialogOptions,fileType,kFileCreator,
  190.                                                                          gGetFilePutFileEventFunctionUPP ,
  191.                                                                          windowRef,&(*docStrucHdl)->modalToWindowNavDialogRef);
  192.  
  193.         if(osError == noErr && (*docStrucHdl)->modalToWindowNavDialogRef != NULL)
  194.         {
  195.             osError = NavDialogRun((*docStrucHdl)->modalToWindowNavDialogRef);
  196.             if(osError != noErr)
  197.             {
  198.                 NavDialogDispose((*docStrucHdl)->modalToWindowNavDialogRef);
  199.                 (*docStrucHdl)->modalToWindowNavDialogRef = NULL;
  200.             }
  201.         }
  202.         
  203.         if(dialogOptions.saveFileName != NULL)
  204.             CFRelease(dialogOptions.saveFileName);
  205.         if(dialogOptions.clientName != NULL)
  206.             CFRelease(dialogOptions.clientName);
  207.     }
  208.  
  209.     return osError;
  210. }
  211.  
  212. // *************************************************************************** doRevertCommand
  213.  
  214. OSErr  doRevertCommand(void)
  215. {
  216.     OSErr                                            osError = noErr;
  217.     NavDialogCreationOptions    dialogOptions;
  218.     WindowRef                                    windowRef;
  219.     Str255                                        windowTitle;
  220.     docStructureHandle                docStrucHdl;
  221.  
  222.     // ………………………………………………………………………………………………………………………… create window-modal Discard Changes dialog
  223.  
  224.     osError = NavGetDefaultDialogCreationOptions(&dialogOptions);
  225.     if(osError == noErr)
  226.     {
  227.         windowRef = FrontWindow();
  228.  
  229.         GetWTitle(windowRef,windowTitle);
  230.         dialogOptions.saveFileName = CFStringCreateWithPascalString(NULL,windowTitle,
  231.                                                                                                                                 CFStringGetSystemEncoding());
  232.         dialogOptions.parentWindow = windowRef;
  233.         dialogOptions.modality = kWindowModalityWindowModal;
  234.  
  235.         docStrucHdl = (docStructureHandle) GetWRefCon(windowRef);
  236.         if((*docStrucHdl)->askSaveDiscardEventFunctionUPP != NULL)
  237.         {
  238.             DisposeNavEventUPP((*docStrucHdl)->askSaveDiscardEventFunctionUPP);
  239.             (*docStrucHdl)->askSaveDiscardEventFunctionUPP = NULL;
  240.         }
  241.         (*docStrucHdl)->askSaveDiscardEventFunctionUPP = 
  242.                                                         NewNavEventUPP((NavEventProcPtr) askSaveDiscardEventFunction); 
  243.  
  244.         HLock((Handle) docStrucHdl);
  245.  
  246.         osError = NavCreateAskDiscardChangesDialog(&dialogOptions,
  247.                                                                                              (*docStrucHdl)->askSaveDiscardEventFunctionUPP,
  248.                                                                                              windowRef,
  249.                                                                                              &(*docStrucHdl)->modalToWindowNavDialogRef);
  250.         HUnlock((Handle) docStrucHdl);
  251.  
  252.         if(osError == noErr && (*docStrucHdl)->modalToWindowNavDialogRef != NULL)
  253.         {
  254.             osError = NavDialogRun((*docStrucHdl)->modalToWindowNavDialogRef);
  255.             if(osError != noErr)
  256.             {
  257.                 NavDialogDispose((*docStrucHdl)->modalToWindowNavDialogRef);
  258.                 (*docStrucHdl)->modalToWindowNavDialogRef = NULL;
  259.             }
  260.         }
  261.         
  262.         if(dialogOptions.saveFileName != NULL)
  263.             CFRelease(dialogOptions.saveFileName);
  264.     }
  265.  
  266.     return osError;
  267. }
  268.  
  269. // **************************************************************************** doNewDocWindow
  270.  
  271. OSErr  doNewDocWindow(Boolean showWindow,OSType documentType,WindowRef * windowRef)
  272. {
  273.     docStructureHandle    docStrucHdl;
  274.     Rect                                portRect;
  275.  
  276.     if(gCurrentNumberOfWindows == kMaxWindows)
  277.         return eMaxWindows;
  278.  
  279.     // …………………………………………………………………………………………………………… create window and associated document structure
  280.  
  281.     if(!(*windowRef = GetNewCWindow(rNewWindow,NULL,(WindowRef)-1)))
  282.         return MemError();
  283.  
  284.     SetPortWindowPort(*windowRef);
  285.  
  286.     if(!(docStrucHdl = (docStructureHandle) NewHandle(sizeof(docStructure))))
  287.     {
  288.         DisposeWindow(*windowRef);
  289.         return MemError();
  290.     }
  291.  
  292.     SetWRefCon(*windowRef,(SInt32) docStrucHdl);
  293.  
  294.     // ………………………………………………………………………………………………………………………………… initialise fields of document structure
  295.  
  296.     (*docStrucHdl)->editStrucHdl                                        = NULL;
  297.     (*docStrucHdl)->pictureHdl                                            = NULL;
  298.     (*docStrucHdl)->fileRefNum                                            = 0;
  299.     (*docStrucHdl)->aliasHdl                                                = NULL;                                                                 /////
  300.     (*docStrucHdl)->windowTouched                                        = false;
  301.     (*docStrucHdl)->modalToWindowNavDialogRef             = NULL;
  302.     (*docStrucHdl)->askSaveDiscardEventFunctionUPP    = NULL;
  303.     (*docStrucHdl)->isAskSaveChangesDialog                    = false;
  304.  
  305.     // ………………… if document's file type is to be 'TEXT', associate TextEdit structure with window
  306.  
  307.     if(documentType == kFileTypeTEXT)
  308.     {
  309.         UseThemeFont(kThemeSmallSystemFont,smSystemScript);
  310.  
  311.         GetWindowPortBounds(*windowRef,&portRect);
  312.         gDestRect = portRect;
  313.         InsetRect(&gDestRect,6,6);
  314.         gViewRect = gDestRect;
  315.  
  316.         MoveHHi((Handle) docStrucHdl);
  317.         HLock((Handle) docStrucHdl);
  318.  
  319.         if(!((*docStrucHdl)->editStrucHdl = TENew(&gDestRect,&gViewRect)))
  320.         {
  321.             DisposeWindow(*windowRef);
  322.             DisposeHandle((Handle) docStrucHdl);
  323.             return MemError();
  324.         }
  325.  
  326.         HUnlock((Handle) docStrucHdl);
  327.     }
  328.  
  329.     // …………………………………………………………………………………………………………………………………… show window and increment window count
  330.  
  331.     if(showWindow)
  332.         ShowWindow(*windowRef);
  333.  
  334.     gCurrentNumberOfWindows ++;
  335.  
  336.     return noErr;
  337. }
  338.  
  339. // ************************************************************************** doCloseDocWindow
  340.  
  341. OSErr  doCloseDocWindow(WindowRef windowRef)
  342. {
  343.     docStructureHandle    docStrucHdl;
  344.     OSErr                                osError = noErr;
  345.  
  346.     docStrucHdl = (docStructureHandle) GetWRefCon(windowRef);
  347.  
  348.     // …………………………………………………………… close file, flush volume, dispose of window and associated memory
  349.  
  350.     if((*docStrucHdl)->fileRefNum != 0)
  351.     {
  352.         if(!(osError = FSClose((*docStrucHdl)->fileRefNum)))    
  353.         {
  354.             osError = FlushVol(NULL,(*docStrucHdl)->fileFSSpec.vRefNum);
  355.             (*docStrucHdl)->fileRefNum = 0;
  356.         }
  357.     }
  358.  
  359.     if((*docStrucHdl)->editStrucHdl != NULL)
  360.         TEDispose((*docStrucHdl)->editStrucHdl);
  361.     if((*docStrucHdl)->pictureHdl != NULL)
  362.         KillPicture((*docStrucHdl)->pictureHdl);
  363.  
  364.     DisposeHandle((Handle) docStrucHdl);
  365.     DisposeWindow(windowRef);
  366.  
  367.     gCurrentNumberOfWindows --;
  368.  
  369.     // …………………………………………………………………………………………………………………………………………………………………………… if quitting application
  370.  
  371.     if(gQuittingApplication)
  372.     {
  373.         if(FrontWindow() == NULL)
  374.             gDone = true;
  375.         else
  376.             doCloseCommand(kNavSaveChangesQuittingApplication);
  377.     }
  378.     
  379.     return osError;
  380. }
  381.  
  382. // ******************************************************************************** doOpenFile
  383.  
  384. OSErr  doOpenFile(FSSpec fileSpec,OSType documentType)
  385. {
  386.     WindowRef                        windowRef;
  387.     OSErr                                osError;
  388.     SInt16                            fileRefNum;
  389.     docStructureHandle    docStrucHdl;
  390.  
  391.     // …………………………………………………………………………………………………………………………………………………………………………………………… create new window
  392.  
  393.     if(osError = doNewDocWindow(false,documentType,&windowRef))
  394.         return osError;
  395.  
  396.     SetWTitle(windowRef,fileSpec.name);    
  397.  
  398.     // ………………………………………………………………………………………………………………………………………………………………………………… open file's data fork
  399.  
  400.     if(osError = FSpOpenDF(&fileSpec,fsRdWrPerm,&fileRefNum))
  401.     {
  402.         DisposeWindow(windowRef);
  403.         gCurrentNumberOfWindows --;
  404.         return osError;
  405.     }
  406.  
  407.     // ………………………………………………… store file reference number and FSSpec in window's document structure
  408.  
  409.     docStrucHdl = (docStructureHandle) GetWRefCon(windowRef);
  410.     (*docStrucHdl)->fileRefNum = fileRefNum;
  411.     (*docStrucHdl)->fileFSSpec = fileSpec;
  412.  
  413.     // ……………………………………………………………………………………………………………………………………………………………………………………………… read in the file
  414.  
  415.     if(documentType == kFileTypeTEXT)
  416.     {
  417.         if(osError = doReadTextFile(windowRef))
  418.             return osError;
  419.     }
  420.     else if(documentType == kFileTypePICT)
  421.     {
  422.         if(osError = doReadPictFile(windowRef))
  423.             return osError;
  424.     }
  425.  
  426.     // ……………………………………………………………………………………………………………………… set up window's proxy icon, and show window
  427.  
  428.     SetWindowProxyFSSpec(windowRef,&fileSpec);                                                                                         /////
  429.     GetWindowProxyAlias(windowRef,&((*docStrucHdl)->aliasHdl));                                                         /////
  430.     SetWindowModified(windowRef,false);                                                                                                         /////
  431.  
  432.     ShowWindow(windowRef);
  433.  
  434.     return noErr;
  435. }
  436.  
  437. // ************************************************************** doCreateAskSaveChangesDialog
  438.  
  439. OSErr  doCreateAskSaveChangesDialog(WindowRef windowRef,docStructureHandle docStrucHdl,
  440.                                                                       NavAskSaveChangesAction action)
  441. {
  442.     OSErr                                            osError;
  443.     NavDialogCreationOptions    dialogOptions;
  444.     Str255                                        windowTitle, applicationName;
  445.  
  446.     // …………………………………………………………………………………………………………… create window-modal Save Changes Changes dialog
  447.  
  448.     osError = NavGetDefaultDialogCreationOptions(&dialogOptions);
  449.     if(osError == noErr)
  450.     {
  451.         GetWTitle(windowRef,windowTitle);
  452.         dialogOptions.saveFileName = CFStringCreateWithPascalString(NULL,windowTitle,
  453.                                                                                                                                 CFStringGetSystemEncoding());
  454.         GetIndString(applicationName,rMiscStrings,sApplicationName);
  455.         dialogOptions.clientName = CFStringCreateWithPascalString(NULL,applicationName,
  456.                                                                                                                             CFStringGetSystemEncoding());
  457.         dialogOptions.parentWindow = windowRef;
  458.         dialogOptions.modality = kWindowModalityWindowModal;
  459.  
  460.         if((*docStrucHdl)->askSaveDiscardEventFunctionUPP != NULL)
  461.         {
  462.             DisposeNavEventUPP((*docStrucHdl)->askSaveDiscardEventFunctionUPP);
  463.             (*docStrucHdl)->askSaveDiscardEventFunctionUPP = NULL;
  464.         }
  465.         (*docStrucHdl)->askSaveDiscardEventFunctionUPP = 
  466.                                                                 NewNavEventUPP((NavEventProcPtr) askSaveDiscardEventFunction);
  467.  
  468.         HLock((Handle) docStrucHdl);
  469.  
  470.         osError = NavCreateAskSaveChangesDialog(&dialogOptions,action,
  471.                                                                                         (*docStrucHdl)->askSaveDiscardEventFunctionUPP,
  472.                                                                                         windowRef,
  473.                                                                                         &(*docStrucHdl)->modalToWindowNavDialogRef);
  474.  
  475.         HUnlock((Handle) docStrucHdl);
  476.  
  477.         if(osError == noErr && (*docStrucHdl)->modalToWindowNavDialogRef != NULL)
  478.         {
  479.             (*docStrucHdl)->isAskSaveChangesDialog = true;
  480.  
  481.             osError = NavDialogRun((*docStrucHdl)->modalToWindowNavDialogRef);
  482.             if(osError != noErr)
  483.             {
  484.                 NavDialogDispose((*docStrucHdl)->modalToWindowNavDialogRef);
  485.                 (*docStrucHdl)->modalToWindowNavDialogRef = NULL;
  486.                 (*docStrucHdl)->isAskSaveChangesDialog = false;
  487.             }
  488.             
  489.             if(!gRunningOnX)
  490.             {
  491.                 if(gCloseDocWindow)
  492.                 {
  493.                     osError = doCloseDocWindow(windowRef);
  494.                     if(osError != noErr)
  495.                         doErrorAlert(osError);
  496.                     gCloseDocWindow = false;
  497.                 }        
  498.             }
  499.         }
  500.  
  501.         if(dialogOptions.saveFileName != NULL)
  502.             CFRelease(dialogOptions.saveFileName);
  503.         if(dialogOptions.clientName != NULL)
  504.             CFRelease(dialogOptions.clientName);
  505.     }
  506.  
  507.     return osError;
  508. }
  509.  
  510. // ************************************************************************* doSaveUsingFSSpec
  511.  
  512. OSErr  doSaveUsingFSSpec(WindowRef windowRef,NavReplyRecord *navReplyStruc)
  513. {
  514.     OSErr                                osError = noErr;
  515.     AEKeyword                        theKeyword;
  516.     DescType                        actualType;
  517.     FSSpec                            fileSpec;
  518.     Size                                actualSize;
  519.     docStructureHandle    docStrucHdl;
  520.     OSType                             fileType;
  521.     CFStringRef                    fileName;
  522.     SInt16                            fileRefNum;
  523.     Rect                                portRect;
  524.  
  525.     if((*navReplyStruc).validRecord)
  526.     {
  527.         // ………………………………………………………………………………………………………………………………………………………………………………………………………… get FSSpec
  528.  
  529.         if((osError = AEGetNthPtr(&(*navReplyStruc).selection,1,typeFSS,&theKeyword,
  530.                                                                 &actualType,&fileSpec,sizeof(fileSpec),&actualSize)) == noErr)
  531.         {
  532.             docStrucHdl = (docStructureHandle) GetWRefCon(windowRef);
  533.  
  534.             // ………………………………… get file name, convert to Pascal string, assign to name field of FSSpec
  535.  
  536.             fileName = NavDialogGetSaveFileName((*docStrucHdl)->modalToWindowNavDialogRef);
  537.             if(fileName != NULL)
  538.                 osError = CFStringGetPascalString(fileName,&fileSpec.name[0],sizeof(FSSpec),
  539.                                                                                     CFStringGetSystemEncoding());
  540.  
  541.             // ………………………………………………………………………………………………………………… if not replacing, first create a new file
  542.  
  543.             if(!((*navReplyStruc).replacing))
  544.             {
  545.                 if((*docStrucHdl)->editStrucHdl != NULL)
  546.                     fileType = kFileTypeTEXT;
  547.                 else if((*docStrucHdl)->pictureHdl != NULL)
  548.                     fileType = kFileTypePICT;
  549.  
  550.                 osError = FSpCreate(&fileSpec,kFileCreator,fileType,(*navReplyStruc).keyScript);
  551.                 if(osError != noErr)
  552.                 {
  553.                     NavDisposeReply(&(*navReplyStruc));
  554.                     return osError;
  555.                 }
  556.             }
  557.  
  558.             // …………………………………………………… assign FSSpec to fileFSSpec field of window's document structure
  559.  
  560.             (*docStrucHdl)->fileFSSpec = fileSpec;
  561.  
  562.             // ………………………………………………………………………………………………… if file currently exists for document, close it
  563.  
  564.             if((*docStrucHdl)->fileRefNum != 0)
  565.             {
  566.                 osError = FSClose((*docStrucHdl)->fileRefNum);
  567.                 (*docStrucHdl)->fileRefNum = 0;
  568.             }
  569.  
  570.             // ……………………………………………………………………………………………………………………………… open file's data fork and write file
  571.             
  572.             if(osError == noErr)
  573.                 osError = FSpOpenDF(&(*docStrucHdl)->fileFSSpec,fsRdWrPerm,&fileRefNum);
  574.  
  575.             if(osError == noErr)
  576.             {
  577.                 (*docStrucHdl)->fileRefNum = fileRefNum;
  578.                 SetWTitle(windowRef,fileSpec.name);
  579.     
  580.                 // … … … … … … … … … … … … … … … … … … … … … proxy icon and file synchronisation stuff
  581.  
  582.                 SetPortWindowPort(windowRef);                                                                                                         /////
  583.                 SetWindowProxyFSSpec(windowRef,&fileSpec);                                                                             /////
  584.                 GetWindowProxyAlias(windowRef,&((*docStrucHdl)->aliasHdl));                                             /////
  585.                 SetWindowModified(windowRef,false);                                                                                             /////
  586.  
  587.                 // … … … … … … … … … … … … … … … … … … … … … … … … … … … …  write file using safe save
  588.  
  589.                 osError = doWriteFile(windowRef);
  590.  
  591.                 NavCompleteSave(&(*navReplyStruc),kNavTranslateInPlace);
  592.             }
  593.         }
  594.     }
  595.  
  596.     SetPortWindowPort(windowRef);
  597.     GetWindowPortBounds(windowRef,&portRect);
  598.     EraseRect(&portRect);
  599.     InvalWindowRect(windowRef,&portRect);
  600.  
  601.     return osError;
  602. }
  603.  
  604. // ************************************************************************** doSaveUsingFSRef
  605.  
  606. OSErr  doSaveUsingFSRef(WindowRef windowRef,NavReplyRecord *navReplyStruc)
  607. {
  608.     OSErr                                osError = noErr;
  609.     AEDesc                            aeDesc;
  610.     Size                                dataSize;
  611.     FSRef                                fsRefParent, fsRefDelete;    
  612.     UniCharCount                nameLength;
  613.     UniChar                            *nameBuffer;
  614.     FSSpec                            fileSpec;
  615.     docStructureHandle    docStrucHdl;
  616.     FInfo                                fileInfo;
  617.     SInt16                            fileRefNum;
  618.     Rect                                portRect;
  619.  
  620.     osError = AECoerceDesc(&(*navReplyStruc).selection,typeFSRef,&aeDesc);
  621.     if(osError == noErr)
  622.     {
  623.         // …………………………………………………………………………………………………………………………………………………………………………………………………………… get FSRef
  624.  
  625.         dataSize = AEGetDescDataSize(&aeDesc);
  626.         if(dataSize > 0)
  627.             osError = AEGetDescData(&aeDesc,&fsRefParent,sizeof(FSRef));
  628.         if(osError == noErr)
  629.         {
  630.             // …………………………………………………………………………… get file name from saveFileName field of NavReplyRecord
  631.  
  632.             nameLength = (UniCharCount) CFStringGetLength((*navReplyStruc).saveFileName);
  633.             nameBuffer = (UniChar *) NewPtr(nameLength);
  634.             CFStringGetCharacters((*navReplyStruc).saveFileName,CFRangeMake(0,nameLength),
  635.                                                         &nameBuffer[0]);
  636.             if(nameBuffer != NULL)
  637.             {
  638.                 // …………………………………………………………………………………………………… if replacing, delete the file being replaced
  639.  
  640.                 if((*navReplyStruc).replacing)
  641.                 {
  642.                     osError = FSMakeFSRefUnicode(&fsRefParent,nameLength,nameBuffer,
  643.                                                                              kTextEncodingUnicodeDefault,&fsRefDelete);
  644.                     {
  645.                         if(osError == noErr)
  646.                             osError = FSDeleteObject(&fsRefDelete);
  647.                         if(osError == fBsyErr)
  648.                         {
  649.                             DisposePtr((Ptr) nameBuffer);
  650.                             return osError;
  651.                         }
  652.                     }
  653.                 }
  654.  
  655.                 // …………………………………… create file with Unicode name (but it can be written with an FSSpec)
  656.  
  657.                 if(osError == noErr)
  658.                 {
  659.                     osError = FSCreateFileUnicode(&fsRefParent,nameLength,nameBuffer,kFSCatInfoNone,
  660.                                                                                 NULL,NULL,&fileSpec);
  661.                     if(osError == noErr)
  662.                     {
  663.                         docStrucHdl = (docStructureHandle) GetWRefCon(windowRef);
  664.  
  665.                         osError = FSpGetFInfo(&fileSpec,&fileInfo);
  666.  
  667.                         if((*docStrucHdl)->editStrucHdl != NULL)
  668.                             fileInfo.fdType = kFileTypeTEXT;
  669.                         else if((*docStrucHdl)->pictureHdl != NULL)
  670.                             fileInfo.fdType = kFileTypePICT;
  671.                         fileInfo.fdCreator = kFileCreator;
  672.  
  673.                         if(osError == noErr)
  674.                             osError = FSpSetFInfo(&fileSpec,&fileInfo);
  675.  
  676.                         (*docStrucHdl)->fileFSSpec = fileSpec;
  677.  
  678.                         // ……………………………………………………………………………………………………………… open file's data fork and write file
  679.  
  680.                         if(osError == noErr)
  681.                             osError = FSpOpenDF(&fileSpec,fsRdWrPerm,&fileRefNum);
  682.  
  683.                         if(osError == noErr)
  684.                         {
  685.                             (*docStrucHdl)->fileRefNum = fileRefNum;
  686.                             SetWTitle(windowRef,fileSpec.name);
  687.  
  688.                             // … … … … … … … … … … … … … … … … … … proxy icon and file synchronisation stuff
  689.  
  690.                             SetPortWindowPort(windowRef);                                                                                             /////
  691.                             SetWindowProxyFSSpec(windowRef,&fileSpec);                                                                 /////
  692.                             GetWindowProxyAlias(windowRef,&((*docStrucHdl)->aliasHdl));                                 /////
  693.                             SetWindowModified(windowRef,false);                                                                                 /////
  694.  
  695.                             // … … … … … … … … … … … … … … … … … … … … … … … … …  write file using safe save
  696.  
  697.                             osError = doWriteFile(windowRef);
  698.  
  699.                             NavCompleteSave(&(*navReplyStruc),kNavTranslateInPlace);
  700.                         }
  701.                     }
  702.                 }
  703.             }
  704.  
  705.             DisposePtr((Ptr) nameBuffer);
  706.         }
  707.  
  708.         AEDisposeDesc(&aeDesc);
  709.     }
  710.     
  711.     SetPortWindowPort(windowRef);
  712.     GetWindowPortBounds(windowRef,&portRect);
  713.     EraseRect(&portRect);
  714.     InvalWindowRect(windowRef,&portRect);
  715.  
  716.     return osError;
  717. }
  718.  
  719. // ******************************************************************************* doWriteFile
  720.  
  721. OSErr  doWriteFile(WindowRef windowRef)
  722. {
  723.     docStructureHandle    docStrucHdl;
  724.     FSSpec                            fileSpecActual, fileSpecTemp;
  725.     UInt32                            currentTime;
  726.     Str255                            tempFileName;
  727.     SInt16                            tempFileVolNum, tempFileRefNum;
  728.     SInt32                            tempFileDirID;
  729.     OSErr                                osError = noErr;
  730.  
  731.     docStrucHdl = (docStructureHandle) GetWRefCon(windowRef);
  732.     fileSpecActual = (*docStrucHdl)->fileFSSpec;
  733.  
  734.     GetDateTime(¤tTime);
  735.     NumToString((SInt32) currentTime,tempFileName);
  736.  
  737.     osError = FindFolder(fileSpecActual.vRefNum,kTemporaryFolderType,kCreateFolder,
  738.                                                 &tempFileVolNum,&tempFileDirID);
  739.     if(osError == noErr)
  740.         osError = FSMakeFSSpec(tempFileVolNum,tempFileDirID,tempFileName,&fileSpecTemp);
  741.     if(osError == noErr || osError == fnfErr)
  742.         osError = FSpCreate(&fileSpecTemp,'trsh','trsh',smSystemScript);
  743.     if(osError == noErr)
  744.         osError = FSpOpenDF(&fileSpecTemp,fsRdWrPerm,&tempFileRefNum);
  745.     if(osError == noErr)
  746.     {
  747.         if((*docStrucHdl)->editStrucHdl != NULL)
  748.             osError = doWriteTextData(windowRef,tempFileRefNum);
  749.         else if((*docStrucHdl)->pictureHdl != NULL)
  750.             osError = doWritePictData(windowRef,tempFileRefNum);
  751.     }    
  752.     if(osError == noErr)
  753.         osError = FSClose(tempFileRefNum);
  754.     if(osError == noErr)
  755.         osError = FSClose((*docStrucHdl)->fileRefNum);
  756.     if(osError == noErr)
  757.         osError = FSpExchangeFiles(&fileSpecTemp,&fileSpecActual);
  758.     if(osError == noErr)
  759.         osError = FSpDelete(&fileSpecTemp);
  760.     if(osError == noErr)
  761.         osError = FSpOpenDF(&fileSpecActual,fsRdWrPerm,&(*docStrucHdl)->fileRefNum);
  762.  
  763.     if(osError == noErr)
  764.         osError = doCopyResources(windowRef);
  765.  
  766.     return osError;
  767. }
  768.  
  769. // **************************************************************************** doReadTextFile
  770.  
  771. OSErr  doReadTextFile(WindowRef windowRef)
  772. {
  773.     docStructureHandle    docStrucHdl;
  774.     SInt16                            fileRefNum;
  775.     TEHandle                        textEditHdl;
  776.     SInt32                            numberOfBytes;
  777.     Handle                            textBuffer;
  778.     OSErr                                osError;
  779.  
  780.     docStrucHdl = (docStructureHandle) GetWRefCon(windowRef);
  781.     fileRefNum = (*docStrucHdl)->fileRefNum;
  782.  
  783.     textEditHdl = (*docStrucHdl)->editStrucHdl;
  784.     (*textEditHdl)->txSize = 10;
  785.     (*textEditHdl)->lineHeight = 15;
  786.  
  787.     SetFPos(fileRefNum,fsFromStart,0);
  788.     GetEOF(fileRefNum,&numberOfBytes);
  789.  
  790.     if(numberOfBytes > 32767)
  791.         numberOfBytes = 32767;
  792.  
  793.     if(!(textBuffer = NewHandle((Size) numberOfBytes)))
  794.         return MemError();
  795.  
  796.     osError = FSRead(fileRefNum,&numberOfBytes,*textBuffer);
  797.     if(osError == noErr || osError == eofErr)
  798.     {
  799.         HLockHi(textBuffer);
  800.         TESetText(*textBuffer,numberOfBytes,(*docStrucHdl)->editStrucHdl);
  801.         HUnlock(textBuffer);
  802.         DisposeHandle(textBuffer);
  803.     }
  804.     else
  805.         return osError;
  806.  
  807.     return noErr;
  808. }
  809.  
  810. // **************************************************************************** doReadPictFile
  811.  
  812. OSErr  doReadPictFile(WindowRef windowRef)
  813. {
  814.     docStructureHandle    docStrucHdl;
  815.     SInt16                            fileRefNum;
  816.     SInt32                            numberOfBytes;
  817.     OSErr                                osError;
  818.  
  819.     docStrucHdl = (docStructureHandle) GetWRefCon(windowRef);
  820.     fileRefNum = (*docStrucHdl)->fileRefNum;
  821.  
  822.     GetEOF(fileRefNum,&numberOfBytes);
  823.     SetFPos(fileRefNum,fsFromStart,512);
  824.     numberOfBytes -= 512;
  825.  
  826.     if(!((*docStrucHdl)->pictureHdl = (PicHandle) NewHandle(numberOfBytes)))
  827.         return MemError();
  828.  
  829.     osError = FSRead(fileRefNum,&numberOfBytes,*(*docStrucHdl)->pictureHdl);
  830.     if(osError == noErr || osError == eofErr)
  831.         return(noErr);
  832.     else
  833.         return osError;
  834. }
  835.  
  836. // *************************************************************************** doWriteTextData
  837.  
  838. OSErr  doWriteTextData(WindowRef windowRef,SInt16 tempFileRefNum)
  839. {
  840.     docStructureHandle    docStrucHdl;
  841.     TEHandle                        textEditHdl;
  842.     Handle                            editText;
  843.     SInt32                            numberOfBytes;
  844.     SInt16                            volRefNum;
  845.     OSErr                                osError;
  846.  
  847.     docStrucHdl = (docStructureHandle) GetWRefCon(windowRef);
  848.     textEditHdl = (*docStrucHdl)->editStrucHdl;
  849.     editText = (*textEditHdl)->hText;
  850.     numberOfBytes = (*textEditHdl)->teLength;
  851.  
  852.     osError = SetFPos(tempFileRefNum,fsFromStart,0);
  853.     if(osError == noErr)
  854.         osError = FSWrite(tempFileRefNum,&numberOfBytes,*editText);
  855.     if(osError == noErr)
  856.         osError = SetEOF(tempFileRefNum,numberOfBytes);
  857.     if(osError == noErr)
  858.         osError = GetVRefNum(tempFileRefNum,&volRefNum);
  859.     if(osError == noErr)
  860.         osError = FlushVol(NULL,volRefNum);
  861.  
  862.     if(osError == noErr)
  863.         (*docStrucHdl)->windowTouched = false;
  864.  
  865.     return osError;
  866. }
  867.  
  868. // *************************************************************************** doWritePictData
  869.  
  870. OSErr  doWritePictData(WindowRef windowRef,SInt16 tempFileRefNum)
  871. {
  872.     docStructureHandle    docStrucHdl;
  873.     PicHandle                        pictureHdl;
  874.     SInt32                            numberOfBytes, dummyData;
  875.     SInt16                            volRefNum;
  876.     OSErr                                osError;
  877.  
  878.     docStrucHdl = (docStructureHandle) GetWRefCon(windowRef);
  879.     pictureHdl = (*docStrucHdl)->pictureHdl;
  880.  
  881.     numberOfBytes = 512;
  882.     dummyData = 0;
  883.  
  884.     osError = SetFPos(tempFileRefNum,fsFromStart,0);
  885.  
  886.     if(osError == noErr)
  887.         osError = FSWrite(tempFileRefNum,&numberOfBytes,&dummyData);
  888.  
  889.     numberOfBytes = GetHandleSize((Handle) (*docStrucHdl)->pictureHdl);
  890.  
  891.     if(osError == noErr)
  892.     {
  893.         HLock((Handle) (*docStrucHdl)->pictureHdl);
  894.         osError = FSWrite(tempFileRefNum,&numberOfBytes,*(*docStrucHdl)->pictureHdl);
  895.         HUnlock((Handle) (*docStrucHdl)->pictureHdl);
  896.     }
  897.  
  898.     if(osError == noErr)
  899.         osError = SetEOF(tempFileRefNum,512 + numberOfBytes);
  900.     if(osError == noErr)
  901.         osError = GetVRefNum(tempFileRefNum,&volRefNum);
  902.     if(osError == noErr)
  903.         osError = FlushVol(NULL,volRefNum);
  904.  
  905.     if(osError == noErr)
  906.         (*docStrucHdl)->windowTouched = false;
  907.  
  908.     return osError;
  909. }
  910.  
  911. // *************************************************************** getFilePutFileEventFunction
  912.  
  913. void  getFilePutFileEventFunction(NavEventCallbackMessage callBackSelector,
  914.                                                                     NavCBRecPtr callBackParms,NavCallBackUserData callBackUD)
  915. {
  916.     OSErr                                osError;
  917.     NavReplyRecord            navReplyStruc;
  918.     NavUserAction                navUserAction;        
  919.     SInt32                            count, index;
  920.     AEKeyword                        theKeyword;
  921.     DescType                        actualType;
  922.     FSSpec                            fileSpec;
  923.     Size                                actualSize;
  924.     FInfo                                fileInfo;
  925.     OSType                            documentType;
  926.     WindowRef                        windowRef;
  927.     AEDesc                            aeDesc;        
  928.     AEKeyword                        keyWord;
  929.     DescType                        typeCode;
  930.     Rect                                theRect;
  931.     Str255                            theString, numberString;
  932.     docStructureHandle    docStrucHdl;
  933.  
  934.     switch(callBackSelector)
  935.     {
  936.         case kNavCBUserAction:
  937.             osError = NavDialogGetReply(callBackParms->context,&navReplyStruc);
  938.             if(osError == noErr && navReplyStruc.validRecord)
  939.             {
  940.                 navUserAction = NavDialogGetUserAction(callBackParms->context);
  941.  
  942.                 switch(navUserAction)
  943.                 {
  944.                     // ……………………………………………………………………………………………………………………… click on Open button in Open dialog
  945.  
  946.                     case kNavUserActionOpen:
  947.                         if(gModalToApplicationNavDialogRef != NULL)
  948.                         {
  949.                             osError = AECountItems(&(navReplyStruc.selection),&count);
  950.                             if(osError == noErr)
  951.                             {
  952.                                 for(index=1;index<=count;index++)
  953.                                 {
  954.                                     osError = AEGetNthPtr(&(navReplyStruc.selection),index,typeFSS,
  955.                                                                                 &theKeyword,&actualType,&fileSpec,sizeof(fileSpec),
  956.                                                                                 &actualSize);
  957.                                     if((osError = FSpGetFInfo(&fileSpec,&fileInfo)) == noErr)
  958.                                     {
  959.                                         documentType = fileInfo.fdType;
  960.                                         osError = doOpenFile(fileSpec,documentType);
  961.                                         if(osError != noErr)
  962.                                             doErrorAlert(osError);
  963.                                     }
  964.                                 }
  965.                             }
  966.                         }
  967.                         break;
  968.  
  969.                     // ……………………………………………………………………………………………… click on Save button in Save Location dialog
  970.  
  971.                     case kNavUserActionSaveAs:
  972.                         windowRef = callBackUD;
  973.                         osError = AECoerceDesc(&navReplyStruc.selection,typeFSRef,&aeDesc);
  974.                         if(osError == noErr)
  975.                         {
  976.                             osError = doSaveUsingFSRef(windowRef,&navReplyStruc);
  977.                             if(osError != noErr)
  978.                                 doErrorAlert(osError);
  979.                             AEDisposeDesc(&aeDesc);
  980.                         }
  981.                         else
  982.                         {
  983.                             osError = doSaveUsingFSSpec(windowRef,&navReplyStruc);
  984.                             if(osError != noErr)
  985.                                 doErrorAlert(osError);
  986.                         }
  987.                         break;
  988.  
  989.                     // …………………………………………………………………………………… click on Choose button in Choose a Folder dialog
  990.  
  991.                     case kNavUserActionChoose:
  992.                         if((osError = AEGetNthPtr(&(navReplyStruc.selection),1,typeFSS,&keyWord,&typeCode,
  993.                                                                             &fileSpec,sizeof(FSSpec),&actualSize)) == noErr)
  994.                         {        
  995.                             FSMakeFSSpec(fileSpec.vRefNum,fileSpec.parID,fileSpec.name,&fileSpec);
  996.                         }
  997.                         windowRef = callBackUD;
  998.                         SetPortWindowPort(windowRef);
  999.                         TextSize(10);
  1000.                         SetRect(&theRect,0,271,600,300);
  1001.                         EraseRect(&theRect);
  1002.                         doCopyPString(fileSpec.name,theString);
  1003.                         doConcatPStrings(theString, "\p   Volume Reference Number: ");
  1004.                         NumToString((SInt32) fileSpec.vRefNum,numberString);
  1005.                         doConcatPStrings(theString,numberString);
  1006.                         doConcatPStrings(theString, "\p   Parent Directory ID: ");
  1007.                         NumToString((SInt32) fileSpec.parID,numberString);
  1008.                         doConcatPStrings(theString,numberString);
  1009.                         MoveTo(10,290);
  1010.                         DrawString(theString);
  1011.                         break;
  1012.                 }
  1013.  
  1014.                 osError = NavDisposeReply(&navReplyStruc);
  1015.             }
  1016.             break;
  1017.  
  1018.         case kNavCBTerminate:
  1019.             if(gModalToApplicationNavDialogRef != NULL)
  1020.             {
  1021.                 NavDialogDispose(gModalToApplicationNavDialogRef);
  1022.                 gModalToApplicationNavDialogRef = NULL;
  1023.             }
  1024.             else
  1025.             {
  1026.                 windowRef = callBackUD;
  1027.                 docStrucHdl = (docStructureHandle) GetWRefCon(windowRef);
  1028.                 if((*docStrucHdl)->modalToWindowNavDialogRef != NULL)
  1029.                 {
  1030.                     NavDialogDispose((*docStrucHdl)->modalToWindowNavDialogRef);
  1031.                     (*docStrucHdl)->modalToWindowNavDialogRef = NULL;
  1032.                 }
  1033.             }
  1034.             break;
  1035.  
  1036.         case kNavCBEvent:
  1037.             switch(callBackParms->eventData.eventDataParms.event->what)
  1038.             {
  1039.                 case updateEvt:
  1040.                     windowRef = (WindowRef) callBackParms->eventData.eventDataParms.event->message;
  1041.                     if(GetWindowKind(windowRef) != kDialogWindowKind)
  1042.                         doUpdate((EventRecord *) callBackParms->eventData.eventDataParms.event);
  1043.                     break;
  1044.             }
  1045.             break;
  1046.     }
  1047. }
  1048.  
  1049. // *************************************************************** askSaveDiscardEventFunction
  1050.  
  1051. void askSaveDiscardEventFunction(NavEventCallbackMessage callBackSelector,
  1052.                                                                  NavCBRecPtr callBackParms,NavCallBackUserData callBackUD)
  1053. {
  1054.     WindowRef                        windowRef;
  1055.     docStructureHandle    docStrucHdl;
  1056.     NavUserAction                navUserAction;
  1057.     Rect                                portRect;
  1058.     OSErr                                osError;
  1059.  
  1060.   switch(callBackSelector)
  1061.     {
  1062.         case kNavCBUserAction:
  1063.             windowRef = callBackUD;
  1064.             docStrucHdl = (docStructureHandle) GetWRefCon(windowRef);
  1065.             if((*docStrucHdl)->modalToWindowNavDialogRef != NULL)
  1066.             {
  1067.                 navUserAction = NavDialogGetUserAction(callBackParms->context);
  1068.                 switch(navUserAction)
  1069.                 {
  1070.                     // ………………………………………………………………………………………………… click on Save button in Save Changes dialog
  1071.  
  1072.                     case kNavUserActionSaveChanges:
  1073.                         doSaveCommand();
  1074.  
  1075.                     // ………………………………………………………………………………… click on Don't Save button in Save Changes dialog
  1076.  
  1077.                     case kNavUserActionDontSaveChanges:
  1078.                         NavDialogDispose((*docStrucHdl)->modalToWindowNavDialogRef);
  1079.                         if(gRunningOnX)
  1080.                         {
  1081.                             osError = doCloseDocWindow(windowRef);
  1082.                             if(osError != noErr)
  1083.                                 doErrorAlert(osError);
  1084.                         }
  1085.                         else
  1086.                             gCloseDocWindow = true;
  1087.                         break;
  1088.  
  1089.                     // ……………………………………………………………………………………………… click on OK button in Discard Changes dialog
  1090.                     
  1091.                     case kNavUserActionDiscardChanges:
  1092.                         GetWindowPortBounds(windowRef,&portRect);
  1093.                         SetPortWindowPort(windowRef);
  1094.                         EraseRect(&portRect);
  1095.  
  1096.                         if((*docStrucHdl)->editStrucHdl != NULL && (*docStrucHdl)->fileRefNum != 0)
  1097.                         {
  1098.                             osError = doReadTextFile(windowRef);
  1099.                             if(osError != noErr)
  1100.                                 doErrorAlert(osError);
  1101.                         }
  1102.                         else if((*docStrucHdl)->pictureHdl != NULL)
  1103.                         {
  1104.                             KillPicture((*docStrucHdl)->pictureHdl);
  1105.                             (*docStrucHdl)->pictureHdl = NULL;
  1106.  
  1107.                             osError = doReadPictFile(windowRef);
  1108.                             if(osError != noErr)
  1109.                                 doErrorAlert(osError);
  1110.                         }
  1111.  
  1112.                         (*docStrucHdl)->windowTouched = false;
  1113.                         SetWindowModified(windowRef,false);                                                                                     /////
  1114.                         InvalWindowRect(windowRef,&portRect);
  1115.  
  1116.                         NavDialogDispose((*docStrucHdl)->modalToWindowNavDialogRef);
  1117.                         (*docStrucHdl)->modalToWindowNavDialogRef = NULL;
  1118.                         break;
  1119.  
  1120.                     // ………………………………………… click on Cancel button in Save Changes or Discard Changes dialog
  1121.  
  1122.                     case kNavUserActionCancel:
  1123.                         if((*docStrucHdl)->isAskSaveChangesDialog == true)
  1124.                         {
  1125.                             gQuittingApplication = false;
  1126.                             (*docStrucHdl)->isAskSaveChangesDialog = false;
  1127.                         }
  1128.                         NavDialogDispose((*docStrucHdl)->modalToWindowNavDialogRef);
  1129.                         (*docStrucHdl)->modalToWindowNavDialogRef = NULL;
  1130.                         break;
  1131.                 }
  1132.             }
  1133.             break;
  1134.  
  1135.         case kNavCBEvent:
  1136.             switch(callBackParms->eventData.eventDataParms.event->what)
  1137.             {
  1138.                 case updateEvt:
  1139.                     windowRef = (WindowRef) callBackParms->eventData.eventDataParms.event->message;
  1140.                     if(GetWindowKind(windowRef) != kDialogWindowKind)
  1141.                         doUpdate((EventRecord *) callBackParms->eventData.eventDataParms.event);
  1142.                     break;
  1143.             }
  1144.             break;
  1145.     }
  1146. }
  1147.  
  1148. // *************************************************************************** doCopyResources
  1149.  
  1150. OSErr  doCopyResources(WindowRef windowRef)
  1151. {
  1152.     docStructureHandle    docStrucHdl;
  1153.     OSType                            fileType;
  1154.     OSErr                                osError = noErr;
  1155.     SInt16                            fileRefNum;
  1156.     Handle                            editTextHdl, textResourceHdl;
  1157.  
  1158.     docStrucHdl = (docStructureHandle) GetWRefCon(windowRef);
  1159.  
  1160.     if((*docStrucHdl)->editStrucHdl)
  1161.         fileType = kFileTypeTEXT;
  1162.     else if((*docStrucHdl)->pictureHdl)
  1163.         fileType = kFileTypePICT;
  1164.  
  1165.     FSpCreateResFile(&(*docStrucHdl)->fileFSSpec,kFileCreator,fileType,smSystemScript);
  1166.  
  1167.     osError = ResError();
  1168.     if(osError == noErr)
  1169.         fileRefNum = FSpOpenResFile(&(*docStrucHdl)->fileFSSpec,fsRdWrPerm);
  1170.  
  1171.     if(fileRefNum > 0)
  1172.     {
  1173.         osError = doCopyAResource('STR ',-16396,gAppResFileRefNum,fileRefNum);
  1174.  
  1175.         if(fileType == kFileTypePICT)
  1176.         {
  1177.             doCopyAResource('pnot',128,gAppResFileRefNum,fileRefNum);
  1178.             doCopyAResource('PICT',128,gAppResFileRefNum,fileRefNum);
  1179.         }
  1180.  
  1181.         if(!gRunningOnX && fileType == kFileTypeTEXT)
  1182.         {
  1183.             doCopyAResource('pnot',129,gAppResFileRefNum,fileRefNum);
  1184.             
  1185.             editTextHdl = (*(*docStrucHdl)->editStrucHdl)->hText;
  1186.             textResourceHdl = NewHandleClear(1024);
  1187.             BlockMoveData(*editTextHdl,*textResourceHdl,1024);            
  1188.             UseResFile(fileRefNum);
  1189.             AddResource(textResourceHdl,'TEXT',129,"\p");
  1190.             if(ResError() == noErr)
  1191.                 UpdateResFile(fileRefNum);
  1192.             ReleaseResource(textResourceHdl);
  1193.         }
  1194.     }
  1195.     else
  1196.         osError = ResError();
  1197.  
  1198.     if(osError == noErr)
  1199.         CloseResFile(fileRefNum); 
  1200.  
  1201.     osError = ResError();
  1202.     return osError;
  1203. }
  1204.  
  1205. // *************************************************************************** doCopyAResource
  1206.  
  1207. OSErr  doCopyAResource(ResType resourceType,SInt16 resourceID,SInt16 sourceFileRefNum,
  1208.                                             SInt16 destFileRefNum)
  1209. {
  1210.     Handle    sourceResourceHdl;
  1211.     Str255    sourceResourceName;
  1212.     ResType    ignoredType;
  1213.     SInt16    ignoredID;
  1214.  
  1215.     UseResFile(sourceFileRefNum);
  1216.  
  1217.     sourceResourceHdl = GetResource(resourceType,resourceID);
  1218.  
  1219.     if(sourceResourceHdl != NULL)
  1220.     {
  1221.         GetResInfo(sourceResourceHdl,&ignoredID,&ignoredType,sourceResourceName);
  1222.         DetachResource(sourceResourceHdl);
  1223.         UseResFile(destFileRefNum);
  1224.         AddResource(sourceResourceHdl,resourceType,resourceID,sourceResourceName);
  1225.         if(ResError() == noErr)
  1226.             UpdateResFile(destFileRefNum);
  1227.     }
  1228.  
  1229.     ReleaseResource(sourceResourceHdl);
  1230.  
  1231.     return ResError();
  1232. }
  1233.  
  1234. // *******************************************************************************************
  1235.